<?php
namespace app\SuperAdmin\controller;
use think\Db;
use think\Controller;
use think\Session;
class ClubsController extends Controller
{
    public function _initialize()
    {
        if (!Session::has('adminid'))
        {
            $this->redirect('index/index/index');
        }
    }

    public function index()
    {   
        $clubs=db("clubs")
        ->order('cid asc')
        ->paginate(5);
        $this->assign('list',$clubs);
        return $this->fetch('clubs');
    }
    public function add(){
        $pic = request()->file('pic');
        if (!empty($pic)) 
        {
            $info = $pic->rule('uniqid')->validate(['size'=>3000000,'ext'=>'jpg,png,gif,jpeg'])->move(ROOT_PATH . 'public' . DS . 'static/img/clubimg');
            if ($info == false) {
                $this->$pic->getError();
            }
        $data=[
        'cname'=>input('cname'),
        'cintro'=>input('cintro'),
        'ctel'=>input('ctel'),
        'copentime'=>input('copentime'),
        'csettime'=>input('csettime'),
        'caccount'=>input('caccount'),
        'cpwd'=>input('cpwd'),
        'caddress'=>input('caddress'),
        'clatitude'=>input('clatitude'),
        'clongitude'=>input('clongitude'),
        'csettime'=>date('Y-m-d H:i:s',time()),
        'cpic'=>$info->getSaveName(),
        ];
   
         if( Db::name("clubs")->insert($data)){
          $this->success('添加成功！');
        }
        else{
          $this->error('添加失败');
        }
        } 
        else 
        {
          $this->error('添加失败');
        }      
    }

    public function delete(){
       $cid=input('id');
       $clubs=Db::name("clubs")->find($cid);
       $pic=$clubs["cpic"];
       $file=ROOT_PATH."public".DS."static".DS. 'img'.DS.'clubimg'.DS.$pic;
       if (file_exists($file)) {
        unlink($file);
       }
       if (Db::table("clubs")->delete($cid)){
         $this->success("删除成功");
       }
       else{
         $this->error("删除失败"); 
       }
    }
}